﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace BeamModule
{
	public partial class SlopeBeamCreateForm : System.Windows.Forms.Form
	{
		public SlopeBeamCreateForm(ExternalCommandData cmdData)
		{
			this.InitializeComponent();
			base.StartPosition = FormStartPosition.Manual;
			if (!this.ReadData())
			{
				base.Location = RevitVersionFuncs.GetRevitTopLeftPoint(cmdData);
				this.tbEndOffset.Text = "0";
			}
		}

		public string EndOffset
		{
			get
			{
				return this.tbEndOffset.Text;
			}
			set
			{
				this.tbEndOffset.Text = value;
			}
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SlopeBeamCreateForm_MouseLeave(object sender, EventArgs e)
		{
			string text = this.tbEndOffset.Text;
			text.Trim();
			if (text.Length == 0)
			{
				this.tbEndOffset.Text = this.m_offset;
				return;
			}
			try
			{
				double num = double.Parse(this.tbEndOffset.Text);
				if (num > 10000.0 || num < -10000.0)
				{
					throw new Exception();
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Show("梁偏移值范围为-10000~10000。");
				this.tbEndOffset.Text = this.m_offset;
				return;
			}
			this.m_offset = this.tbEndOffset.Text;
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				SlopeBeamCreateForm.SendMessage(CmdSlopeBeamCreate._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SlopeBeamCreateForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			SlopeBeamCreateForm.SendMessage(CmdSlopeBeamCreate._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			SlopeBeamCreateForm.keybd_event(bVk, 0, dwFlags, 0);
			SlopeBeamCreateForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void SlopeBeamCreateForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void SlopeBeamCreateForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "SlopeBeam.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_offset);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "SlopeBeam.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
				this.m_offset = streamReader.ReadLine();
				double num = double.Parse(this.tbEndOffset.Text);
				if (num > 10000.0 || num < -10000.0)
				{
					this.m_offset = "0";
				}
				this.tbEndOffset.Text = this.m_offset;
			}
			catch
			{
				return false;
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
			return true;
		}

		private void tbEndOffset_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ('-' == e.KeyChar || '.' == e.KeyChar)
			{
				return;
			}
			if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private void tbEndOffset_TextChanged(object sender, EventArgs e)
		{
			if (1 == this.tbEndOffset.Text.Length && this.tbEndOffset.Text.CompareTo("-") == 0)
			{
				return;
			}
			try
			{
				double num = double.Parse(this.tbEndOffset.Text);
				if (num > 10000.0 || num < -10000.0)
				{
					throw new Exception();
				}
			}
			catch (Exception)
			{
				TaskDialog.Show("盈建科", "梁偏移值范围为-10000~10000。");
				this.tbEndOffset.Text = this.m_offset;
				return;
			}
			this.m_offset = this.tbEndOffset.Text;
		}

		public string m_offset = "0";
	}
}
